﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace OALPTool
{
    public partial class FrmDgvSet : Form
    {
        private string CorpNo = "";
        private string UserNo = "";
        DataView dv = new DataView();

        public FrmDgvSet()
        {
            InitializeComponent();
        }

        public int InitForm(out string strMsg)
        {
            strMsg = "";
            //CorpNo = ISysPlatformFunc.GetCorpNo();
            // UserNo = ISysPlatformFunc.GetUserNo();
            return ReadFromDB(out strMsg);
        }

        private int ReadFromDB(out string strMsg)
        {
            strMsg = "";
            DataSet ds = null;
            string strsql = "";

            int ret = DgvColEdit.ReadDgFuncSort(CorpNo, UserNo, "SAL_YGSX", strsql, out ds, out strMsg);

            if (ret != 0) return ret;

            if (ds == null || ds.Tables.Count == 0)
            {
                TSysPublic.ShowMsg("null");
                strMsg = "获取样式列表为空!";
                return -1;

            }

            dv = ds.Tables[0].DefaultView;

            this.SetDgStyle();
            return 0;
        }

        private void SetDgStyle()
        {
            DataGridViewFunc.setDgvStyle(this.getDgvStyle(), ref this.dgv, dv);

            this.dgv.AutoGenerateColumns = false;
            this.dgv.ShowCellToolTips = true;
            this.dgv.AllowUserToAddRows = false;
            this.dgv.AllowUserToDeleteRows = false;
            this.dgv.AllowUserToOrderColumns = false;
            this.dgv.ColumnHeadersHeight = 25;
            this.dgv.RowTemplate.Height = 23;
            this.dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dgv.MultiSelect = false;
        }

        private ArrayList getDgvStyle()
        {
            ArrayList colList = new ArrayList();
            colList.Add(new object[] { "表格项目", "FieldNameCh", "0", 80, null, DataGridViewContentAlignment.MiddleCenter, true, "无", null, 1, "", "", "", "", "" });
            //colList.Add(new object[] { "字段英文名", "FieldName", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 2, "", "", "", "", "" });
            colList.Add(new object[] { "是否可见", "ViewFlag", "1", 80, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 2, "", "", "", "", "" });
            //colList.Add(new object[] { "显示序号", "ViewSeqNo", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 4, "", "", "", "", "" });
            //colList.Add(new object[] { "排序序号", "SortNo", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "无", null, 5, "", "", "", "", "" });
            //colList.Add(new object[] { "排序方式", "SortFlag", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 6, "", "", "", "", "" });
            //colList.Add(new object[] { "控件类型", "CtrlType", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 7, "", "", "", "", "" });
            colList.Add(new object[] { "显示宽度", "ViewWidth", "0", 80, null, DataGridViewContentAlignment.MiddleRight, false, "", null, 3, "", "", "", "", "" });
            //colList.Add(new object[] { "显示格式化", "ViewFormat", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "无", null, 9, "", "", "", "", "" });
            //colList.Add(new object[] { "对齐方式", "ViewAlign", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 10, "", "", "", "", "" });
            //colList.Add(new object[] { "能否修改", "EditAble", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 11, "", "", "", "", "" });
            //colList.Add(new object[] { "数据为空时显示", "nullText", "0", 100, null, DataGridViewContentAlignment.MiddleCenter, false, "", null, 12, "", "", "", "", "" });

            return colList;
        }

        private void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (this.dgv.Columns[e.ColumnIndex].Name == "ViewWidth")
            {
                if (e.FormattedValue.ToString().Trim() == String.Empty)
                {
                    TSysPublic.ShowMsg("宽度不能为空，必须为大于0的整数！");
                    this.dgv.Rows[e.RowIndex].ErrorText = "宽度不能为空，必须为大于0的整数！";
                    e.Cancel = true;
                }
                else if (!IsNum(e.FormattedValue.ToString()))
                {
                    TSysPublic.ShowMsg("宽度不能为空，必须为大于0的整数！");
                    this.dgv.Rows[e.RowIndex].ErrorText = "宽度不能为空，必须为大于0的整数！";
                    e.Cancel = true;
                }
                else
                {
                    this.dgv.Rows[e.RowIndex].ErrorText = "";
                    e.Cancel = false;
                }

            }
        }

        public bool IsNum(string str)
        {
            System.Text.RegularExpressions.Regex reg1
                = new System.Text.RegularExpressions.Regex(@"^([1-9][0-9]*)$");
            return reg1.IsMatch(str);
        }

        private void tbbUp_Click(object sender, EventArgs e)
        {
            if (this.dgv.CurrentRow == null)
            {
                return;
            }
            else if (this.dgv.CurrentRow.Index == 0)
            {
                return;
            }
            else
            {
                DataTable dt = dv.Table;
                DataTable dt_copy = dt.Copy();
                int currow = this.dgv.CurrentRow.Index;

                string strFname = dt.Rows[currow]["FieldNameCh"].ToString();
                string strVflag = dt.Rows[currow]["ViewFlag"].ToString();
                string strVwidth = dt.Rows[currow]["ViewWidth"].ToString();

                dt_copy.Rows.RemoveAt(currow);

                DataRow cur_dr = dt_copy.NewRow();
                cur_dr["FieldNameCh"] = strFname;
                cur_dr["ViewFlag"] = strVflag;
                cur_dr["ViewWidth"] = strVwidth;

                dt_copy.Rows.InsertAt(cur_dr, currow - 1);
                dt_copy.AcceptChanges();

                dv = dt_copy.DefaultView;

                this.SetDgStyle();

                this.dgv.ClearSelection();
                this.dgv.Rows[currow - 1].Selected = true;
                this.dgv.CurrentCell = this.dgv.Rows[currow - 1].Cells[0];
            }
        }

        private void tbbDown_Click(object sender, EventArgs e)
        {
            if (this.dgv.CurrentCell == null)
            {
                return;
            }
            else if (this.dgv.CurrentRow.Index == this.dgv.Rows.Count - 1)
            {
                return;
            }
            else
            {
                DataTable dt = dv.Table;
                DataTable dt_copy = dt.Copy();
                int currow = this.dgv.CurrentRow.Index;

                string strFname = dt.Rows[currow]["FieldNameCh"].ToString();
                string strVflag = dt.Rows[currow]["ViewFlag"].ToString();
                string strVwidth = dt.Rows[currow]["ViewWidth"].ToString();

                dt_copy.Rows.RemoveAt(currow);

                DataRow cur_dr = dt_copy.NewRow();
                cur_dr["FieldNameCh"] = strFname;
                cur_dr["ViewFlag"] = strVflag;
                cur_dr["ViewWidth"] = strVwidth;

                dt_copy.Rows.InsertAt(cur_dr, currow + 1);
                dt_copy.AcceptChanges();

                dv = dt_copy.DefaultView;

                this.SetDgStyle();

                this.dgv.ClearSelection();
                this.dgv.Rows[currow + 1].Selected = true;
                this.dgv.CurrentCell = this.dgv.Rows[currow + 1].Cells[0];
            }
        }

        private void tbbSave_Click(object sender, EventArgs e)
        {
            ArrayList arr_strsql = new ArrayList();
            foreach (DataGridViewRow dvr in this.dgv.Rows)
            {
            }
        }

        private void tbbExit_Click(object sender, EventArgs e)
        {
            if (TSysPublic.ShowOkMsg("是否退出？") == 1)
            {
                this.Close();
            }
        }

    }
}
